import 'package:decimal/decimal.dart';
import 'package:flutter/material.dart';

/// 通用工具类
class XUtils {
  XUtils._inner();

  static XUtils? _instance;

  factory XUtils() => _instance ??= XUtils._inner();

  /// 颜色转换
  /// Color类需要这样的Color(0xFF00000)，因此我需要将"#AABBCC"转换为0xFFAABBCC
  Color hexToColor(String? hexString, {Color defaultColor = Colors.white}) {
    if (hexString == null || hexString.isEmpty) {
      return defaultColor;
    }

    if (hexString.length == 7) {
      var value = int.tryParse(hexString.substring(1, 7), radix: 16);
      if (value == null) {
        return defaultColor;
      }

      return Color(value | 0xFF000000);
    } else if (hexString.length == 9) {
      //有透明度，透明度在前两位 如："#FFAABBCC"
      var value = int.tryParse(hexString.replaceFirst('#', ''), radix: 16);
      if (value == null) {
        return defaultColor;
      }
      return Color(value);
    } else {
      return defaultColor;
    }
  }

  /// 颜色转换
  /// 把颜色转换为字符串(提交接口数据时使用)
  String colorToHex(Color color, {int digit = 8}) {
    String colorStr = color.value.toRadixString(16);
    if (digit == 6) {
      if (colorStr.length == 8 && colorStr.startsWith('FF')) {
        colorStr = colorStr.substring(2);
      }
    }
    return '#$colorStr';
  }

  /// String转最多保留几位小数的String
  static String? stringToFormatString(String? value, {int? scale}) {
    return Decimal.tryParse(value ?? '')?.round(scale: scale ?? 2).toString() ??
        value;
  }

  /// double转最多保留几位小数的double
  static double? doubleToFormatDouble(double? value, {int? scale}) {
    return Decimal.tryParse(value.toString())
        ?.round(scale: scale ?? 2)
        .toDouble();
  }

  /// String转最多保留几位小数的double
  static double? stringToFormatDouble(String? value, {int? scale}) {
    return Decimal.tryParse(value ?? '')?.round(scale: scale ?? 2).toDouble();
  }

  /// double转最多保留几位小数的String
  static String? doubleToFormatString(double? value, {int? scale}) {
    return Decimal.tryParse(value.toString())
        ?.round(scale: scale ?? 2)
        .toString();
  }

  /// 计算内容宽度
  static double countTextPaintWidth(String text, double fontSize,
      {double? maxWidth, double? minWidth, int maxLine = 1}) {
    final TextPainter textPainter = TextPainter(
        text: TextSpan(
            text: text,
            style: TextStyle(
              fontSize: fontSize,
            )),
        textDirection: TextDirection.ltr,
        maxLines: maxLine)
      ..layout(minWidth: minWidth ?? 0, maxWidth: maxWidth ?? double.infinity);
    return textPainter.size.width;
  }

  /// 计算内容高度
  static double countTextPaintHeight(String text, double fontSize,
      {double? maxWidth, double? minWidth, int maxLine = 1}) {
    final TextPainter textPainter = TextPainter(
        text: TextSpan(
            text: text,
            style: TextStyle(
              fontSize: fontSize,
            )),
        textDirection: TextDirection.ltr,
        maxLines: maxLine)
      ..layout(minWidth: minWidth ?? 0, maxWidth: maxWidth ?? double.infinity);
    return textPainter.size.height;
  }
}
